package edu.cuit.avatar.array;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/3 14:51
 */
public class ArrayTest {
    public static int sum(int num1, int num2){
        return num1 + num2;
    }

    public static int sum(int[] list){
        if(list == null) return 0;
        int sum = 0;
        for(int e : list)
            sum += e;
        return sum;
    }

    /**
     * 一个方法只能有一个可变参数且只能出现在最后位置
     * @param list
     * @return
     */
    public static int sum2(int... list){
        if(list == null) return 0;
        int sum = 0;
        for(int e : list)
            sum += e;
        return sum;
    }

    public static void main(String[] args) {
        //0个数据
        System.out.println(sum(null));
        System.out.println(sum(new int[]{}));
        //1个数据
        System.out.println(sum(new int[]{10}));
        //5个数据
        System.out.println(sum(new int[]{10, 20, 30, 40, 50}));
        System.out.println("===========================");

        System.out.println(sum2());
        System.out.println(sum2(10));
        System.out.println(sum2(10, 20));
        System.out.println(sum2(10, 20, 30));
        System.out.println(sum2(10, 20, 30, 40));
    }
}
